package com.ybl.library;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.List;

/**
 * 日期：16/6/24 11:48
 * <p/>
 * 描述：
 */
public class TradeResultStepView extends LinearLayout implements TradeResultStepViewIndicator.OnDrawIndicatorListener {
    private RelativeLayout mTextContainer;
    private TradeResultStepViewIndicator mStepsViewIndicator;
    private List<View> mTexts;


    public TradeResultStepView(Context context) {
        this(context, null);
    }

    public TradeResultStepView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TradeResultStepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.widget_trade_result_stepsview, this);
        mStepsViewIndicator = (TradeResultStepViewIndicator) rootView.findViewById(R.id.steps_indicator);
        mStepsViewIndicator.setOnDrawListener(this);
        mTextContainer = (RelativeLayout) rootView.findViewById(R.id.rl_text_container);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 设置显示的文字
     *
     * @param texts
     * @return
     */
    public TradeResultStepView setStepViewTexts(List<View> texts) {
        mTexts = texts;
        if (texts != null) {
            mStepsViewIndicator.setStepNum(mTexts.size());
        } else {
            mStepsViewIndicator.setStepNum(0);
        }
        return this;
    }


    /**
     * 设置StepsViewIndicator默认图片
     *
     * @param smallIcon
     */
    public TradeResultStepView setStepsViewIndicatorSmallIcon(Drawable smallIcon) {
        mStepsViewIndicator.setSmallIcon(smallIcon);
        return this;
    }

    /**
     * 设置StepsViewIndicator已完成图片
     *
     * @param bigIcon
     */
    public TradeResultStepView setStepsViewIndicatorBigIcon(Drawable bigIcon) {
        mStepsViewIndicator.setmBigIcon(bigIcon);
        return this;
    }


    @Override
    public void ondrawIndicator() {
        if (mTextContainer != null) {
            mTextContainer.removeAllViews();//clear ViewGroup
            List<Float> complectedXPosition = mStepsViewIndicator.getCircleCenterPointPositionList();
            if (mTexts != null && complectedXPosition != null && complectedXPosition.size() > 0) {
                for (int i = 0; i < mTexts.size(); i++) {
                    View childView = mTexts.get(i);
                    ViewGroup.LayoutParams params =
                            new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    childView.setY(complectedXPosition.get(i)
                            - (i == 0 ? mStepsViewIndicator.getCircleBigRadius() / 2 : mStepsViewIndicator.getCircleSmallRadius() / 2)
                            - 14);
                    childView.setLayoutParams(params);
                    mTextContainer.addView(childView);
                }
            }
        }
    }
}
